import datetime
now = datetime.datetime.now()

from makan import *

def call():
    return service()
    
def test():
    from xmlrpclib import ServerProxy
    s = ServerProxy('http://127.0.0.1:8000/makan/comment/call/xmlrpc')
    p = s.xGetComments( 22 , 22)
    return  dict(p = p)
    
    
    
def coreAddComment( lat , long , uid , content):
        chk =   []
        #get placeId
        q2 = db.places.long == long
        q3 = db.places.lat == lat
        placeId = db(q2 & q3).select().first()
        #get userId
        userId= db(db.users.id == uid).select().first()
        #add comment to comment table
        commentId = db.comments.insert(user = userId ,content = content, datetime=now )
        #insert comment in comments table
        place_comment =  db(q2 & q3).select(db.places.comments).first()
        
        #insert comment in place
        for comment in place_comment.comments :
                    chk.append(comment)
        chk.append(commentId)
        added  =  db(db.places.id == placeId ).update( comments =  chk )
        
        if placeid :
            msg = "Added"
        else:
            msg= "Error"       
        return msg
@service.xmlrpc
def xAddComment(  lat , long , uid , content):
        return coreAddComment(  lat , long , uid , content)

@auth.requires_login()
def addComment():
      if request.post_vars:
        vars = request.post_vars
        uid = vars.uid
        long= float(vars.long)
        lat= float(vars.lat)    
        content = vars.content
        return dict( added = coreAddComment(  lat , long , uid , content))
####################
def coreGetComments(  lat , long ):
    
    comment = {}
    comment_data = []
    q2 = db.places.long == long
    q3 = db.places.lat == lat
    #select comment for a given place
    place_comments =  db(q2 & q3).select(db.places.comments).first()
      
    result = {}
  
    for c in place_comments.comments :
    #get comment data
            com = db.comments[c] 
            user =  db.users[com.user]
           
            result[str(c)] = { "content": com.content , "firstname" : user.first_name , "lastname" : user.last_name }
           
            
          
            
       #make this in anther function
       #get user data who make this comment
       # user_comment  = db(db.users.id == comment_data.user).select().first()
       #user_list.append(user_comment)

    return result 
    
@service.xmlrpc
def xGetComments( lat , long ):
        c = coreGetComments( lat , long )
        d = jsonDictOfDicts(c)
        return d
                
@auth.requires_login()
def GetComments():
      if request.post_vars:
        vars = request.post_vars
      
        long= float(vars.long)
        lat= float(vars.lat)    
    
        return coreGetComments( lat , long )

###################

def coreCommentDelete(cid  , uid):
        chk = []
        deleted = 0 
     
        #get user who comment
        comment_user = db(db.comments.id == cid).select(db.comments.user).first()

        #delete from comment table
        del_comment = db(db.comments.id == cid ).delete()
       #delete from places table
        comments = db(db.places.comments.contains(cid)).select().first()
        if comments :
            for comment in comments.comments :
                chk.append(comment)
            chk.remove(int(cid))
            
            deleted  =  db(db.places.id == comments.id ).update( comments =  chk )
            if deleted :
                msg = "deleted"
            else:
                msg= "Error"       
            return dict( msg = msg )
        

@service.xmlrpc
def xCommentDelete( cid , uid ):
        result = coreCommentDelete( cid , uid )
        
        return jsonString(result)

        
@auth.requires_login()
def commentDelete():
      if request.post_vars:
        vars = request.post_vars
        cid= vars.cid
        uid = vars.uid
    
        return coreCommentDelete( cid , uid )
